var showFees = function(outData) {
	if (!outData) {
		alert("费率数据为空！");
		return;
	}

	if (outData.VFEE_INFO == null) {
		alert("费率数据为空！");
		return;
	}

	console.info(outData);

	var vw = outData.VFEE_INFO; //outData.VFEE_INFO instanceof Object?[{}]:outData.VFEE_INFO;
	console.info(vw);
	if (vw.length == 0) { //查询成功但是返回资费列表info为空
		var str1 = "<option value='-1'>查无资费</option>";
		$("#fee").empty().append(str1);
		return;
	}
	var str = "<option value='-1'>请选择V网资费</option>";
	for (var i = 0; i < vw.length; i++) {
		var feeName = vw[i].FEE_NAME.substring(vw[i].FEE_NAME.indexOf('>') + 1, vw[i].FEE_NAME.length);
		str += "<option value=\"" + vw[i].FEE_NUM + "\">" + feeName + "</option>";
	}
	$("#fee").children().remove();
	$("#fee").append(str);

};

var loadFees = function() {
	//TODO: 去掉PHONE_NO
	var params = {
		"BODY": {
			"PHONE_NO": getMobile(),
			"WORN_SERV_CODE": "sVpmnFCodeCfm",
			"LOGIN_NO": getEmpCode(),
			"COMMON_INFO": {
				"PROVINCE_GROUP": "10008"
			}
		}
	};
	sendRestPostAjax(params, Constants.rpcCrmHost + "sUserOrdQry?token=" + getToken(), showFees);
};

var checkPhone = function(phone) {
	var reg = Constants.cmccMobileReg; // /^1[3|4|5|7|8]\\d{9}$/;
	if (reg.test(phone)) {
		return true;
	}
	return false;
};

var checkShortNum = function(shortNum) {
	if ((shortNum) && (shortNum.length > 2) && (shortNum.length < 7) && (shortNum.substring(0, 1) == 6)) {
		return true;
	}
	return false;
};

var checkFee = function(fee) {
	if ((fee) && (fee != "-1")) {
		return true;
	}
	return false;
};

var saveOrder = function() {
	var orderParams = {
		'PHONE_NO': $("#phone").val(),
		'LOGIN_NO': getEmpCode(),
		'OPERATE_TYPE': '0',
		'BUSINESS_CODE': getParamValue("vcode"),
		'BUSINESS_NAME': decodeURI(getParamValue('vname')),
		'PKG_TYPE': $("#fee").val(),
		'PKG_NAME': decodeURI($('#fee option:selected').text()),
		'SHORT_NO': $("#shortNum").val()
	};
	console.log("saveOrder参数：", orderParams);
  $.ajax({
      type: "post",
      contentType: "application/json",
      data: JSON.stringify(orderParams),
      url: Constants.apiV2Host+"h5/order/vmember/save",
      dataType: "json",
      async: true,
      headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
        if ('' + data.ResultCode !== '1000') {
          console.log(data.Message);
          alert("保存订单失败！");
        }
      },
      error: function(textStatus) {
        alert("保存订单失败！服务器没有返回。");
        console.info("request error:" + textStatus);
      }
  })
}

var addCallback = function() {
	alert("添加V网成员" + $("#phone").val() + "成功！");
	saveOrder();
	$("#phone").val("");
	$("#shortNum").val("");

};

var add = function() {
	var params = {
		"BODY": {
			"WORN_SERV_CODE": "sEsopVMbrAdd",
			"COMMON_INFO": {
				"PROVINCE_GROUP": "10008"
			},
			"UNIT_ID": getParamValue("vcode"),
			"PHONE_NO": $("#phone").val(),
			"PKG_TYPE": $("#fee").val(),
			"SHORT_NO": $("#shortNum").val(),
			"LOGIN_NO": getEmpCode(),
			"IMEI_NO": ""
		}
	};
	sendRestPostAjax(params, Constants.rpcCrmHost + "s4000Cfm?token=" + getToken(), addCallback);
};

var searchCallback = function() {
	alert("添加V网成员" + $("#phone").val() + "成功！");
	$("#phone").val("");
	$("#shortNum").val("");

};


var search = function(type, key) {
	var groupId = getParamValue("groupId");
	//TODO: 去掉PHONE_NO
	var params = {
		"BODY": {
			"PHONE_NO": getMobile(),
			"WORN_SERV_CODE": "sVpmnQryMbr",
			"COMMON_INFO": {
				"PROVINCE_GROUP": "10008"
			},
			"UNIT_ID": getParamValue("vcode"),
			"PARAM_TYPE": type,
			"MEB_NO": key,
			"LOGIN_NO": getEmpCode()
		}
	};
	sendRestPostAjax(params, Constants.rpcCrmHost + "sUserOrdQry?token=" + getToken(), searchCallback);
};

$(function() {
	console.log("vcode:" + getParamValue("vcode"));
	$("#vCode").html(getParamValue("vcode"));

	$("#submit").on("click", function() {
		var phone = $("#phone").val();
		var shortNum = $("#shortNum").val();
		if (!checkPhone(phone)) {
			if(phone.length==11){
				alert("仅支持移动手机号码！");
			}else{
				alert("请输入11位移动手机号码！");
			}

			return;
		}
		if (!checkShortNum(shortNum)) {
			alert("请输入以6开头且6位数以内的短号，如654321");
			return;
		}

		if (!checkFee($("#fee").val())) {
			alert("请选择V网资费");
			return;
		}

		add();

	});

	$("#search").on("click", function() {
		var key = $("#key").val();
		var type = null;
		if (key.length == 11) { //手机号
			if(checkPhone(key)){
				type = 1;	
			}else{
				alert("请输入移动手机号码！");
				return;
			}

		} else if ((key.length >2)&&(key.length < 7)) { //短号
			type = 2;
		} else {
			alert("请输入11位移动手机号码或3到6位短号！");
			return;
		}

		search(type, key);

	});

	loadFees();
});